package com.item.model.annocation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zhang-rongyao
 * @version V1.0
 * @Package com.item.model.annocation
 * @date 2021/7/22 16:49
 */
@Component
@Aspect
public class RequestLogAspect {

    private static final Logger log = LoggerFactory.getLogger(RequestLogAspect.class);

    //这里需要注意了，这个是将自己自定义注解作为切点的根据，路径一定要写正确了
    @Pointcut(value = "@annotation(com.item.model.annocation.RequestLog)")
    public void access() {
    }

    //进来切点世界，先经过的第一个站
    @Before("access()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }


    //环绕增强，是在before前就会触发
    //注解里的参数(requestLog)一定要与方法参数一致，否则报错
    @Around("@annotation(requestLog)")
    public Object around(ProceedingJoinPoint pjp, RequestLog requestLog) throws Throwable {
        log.info("-aop 日志环绕阶段-[{}]",new Date());
        // TODO Auto-generated method stub
        return pjp.proceed();
    }


    //进来切点这，最后经过的一个站，也是方法正常运行结束后
    @After("access()")
    public void after(JoinPoint joinPoint) {
        log.info("-aop 日志结束-[{}]",new Date());
    }

}